﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using Grundy.Common;
using Grundy.Entity;
using Grundy.Message;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Grundy.Licensing.Server.Tests
{
    [TestClass]
    public class ExecutionCountedConcurrentSessionsStrategyTests
    {
        private License license;
        private ExecutionCounter executionCounter;

        [TestInitialize]
        public void Setup()
        {
            executionCounter = new ExecutionCounter(null, new TimeSpan(0, 0, 0, 300));
            license = new CountedFloatingLicense { Client = LicenseClientType.Machine, LimitType = FloatingLicenseLimitType.Execution, Limit = 1 };
        }

        [TestMethod]
        public void Should_increment_execution_counter()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new ExecutionCountConcurrentSessionsStrategy(executionCounter, license, LicenseEntitlementValidity.Null, fik);
            var clientSessions = new ConcurrentDictionary<Guid, ClientSession>();
            var sessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
            Assert.AreEqual(1,executionCounter.GetExecutionCount(fik).Executions);
        }

        [TestMethod]
        [ExpectedException(typeof(CountedFloatingLicensesExhaustedException))]
        public void Should_not_add_session_to_client_sessions_if_limit_exceeded()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new ExecutionCountConcurrentSessionsStrategy(executionCounter ,license, LicenseEntitlementValidity.Null, fik);
            var clientSessions = new ConcurrentDictionary<Guid, ClientSession>();
            strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
            strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = new IdentifierCollection(), ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());
        }


        [TestMethod]
        public void Should_not_add_session_to_client_sessions_as_they_are_stored_in_execution_count()
        {
            var fik = new FeatureIdentificationKey();
            var strategy = new ExecutionCountConcurrentSessionsStrategy(executionCounter, license, LicenseEntitlementValidity.Null, fik);
            var sessionId = Guid.NewGuid();
            var clientSessions = new ConcurrentDictionary<Guid, ClientSession>();
            var identifierCollection = new IdentifierCollection();
            var returnedSessionId = strategy.Checkout(new LicenseRequestInformation { ClientIdentifiers = identifierCollection, ClientSessionId = Guid.NewGuid(), FeatureIdentificationKey = fik }, new object());

            Assert.AreEqual(0, clientSessions.Count);
        }
    }
}